package bancosys.tec.datadigester.fileregistry.services.incluir;

import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Parameter;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.RawInput;
import bancosys.tec.datadigester.fileregistry.domain.FileRegistry;
import bancosys.tec.datadigester.fileregistry.domain.enumx.Frequency;
import bancosys.tec.persist.dao.BaseDAO;

/**
 * @author piercio
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class FileRegistryService {

    private static final String FILE_CODE = "File code";

    private static final String DRIVER_CLASS = "Driver class";

    private static final String PROCESSOR_ID = "Processor ID";

    private static final String FREQUENCY = "Frequency";

    private static final String MONTH_DAY = "Month day";

    public static final String DATABASE_ID = "Database ID";

    private RawInput serviceProperties;

    /**
     * Adiciona um novo registro de importação de arquivo.
     * 
     * @param dao o {@link BaseDAO} para o arquivo.
     * @param databaseID o id na base.
     * @param fileCode o código do arquivo.
     * @param driverClass a classe do driver para o arquivo.
     * @param processorId o id do bean do processor do arquivo.
     * @param frequency a frequencia de importação.
     * @param monthDay o dia do mês (no caso de frequencia mensal).
     */
    @Execution
    @SuppressWarnings("deprecation")
    public void execute(@Injected BaseDAO<FileRegistry> dao, @Input(fieldName = DATABASE_ID, required = false) Long databaseID,
            @Parameter(value = FILE_CODE) String fileCode, @Parameter(value = DRIVER_CLASS) String driverClass,
            @Parameter(value = PROCESSOR_ID) String processorId, @Parameter(value = FREQUENCY) Frequency frequency,
            @Input(fieldName = MONTH_DAY, required = false) Integer monthDay) {
        FileRegistry registry = dao.createBean();
        registry.setFileCode(this.serviceProperties.getRaw(FILE_CODE));
        registry.setDriverClass(this.serviceProperties.getRaw(DRIVER_CLASS));
        registry.setProcessorId(this.serviceProperties.getRaw(PROCESSOR_ID));
        registry.setFrequency(frequency);
        if (monthDay != null) {
            registry.setMonthDay(monthDay);
        }
        if (databaseID != null) {
            registry.getController().getSessionFactory().getCurrentSession().save(registry, databaseID);
        }
        registry.getPersister().save();
    }

    /**
     * @param serviceProperties o valor para o campo serviceProperties.
     */
    @Injected()
    public void setServiceProperties(RawInput serviceProperties) {
        this.serviceProperties = serviceProperties;
    }
}
